class MembershipDatesController < ApplicationController

  #
  #
  def create
    @membership = Membership.find(params[:new_date][:id])
	new_membership_date = MembershipDate.new(:event => params[:new_date][:event])
	new_membership_date.membership_id = @membership.id
	
	
	new_date = FullDate.find(:first, :conditions => ["day = ? AND month = ? AND year = ?",
                                    	params[:new_date][:day], params[:new_date][:month], params[:new_date][:year]])
										
	#
	if new_date.nil?
	  new_membership_date.build_full_date(:day => params[:new_date][:day],
	                                  :month => params[:new_date][:month],
									  :year => params[:new_date][:year])
	else
	  new_memebership_date.full_date = new_date
	end
	
	#
	respond_to do |format|
	  if new_membership_date.save
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html("membership-dates", :partial => 'dates/dates',
											 :locals => { :dates => @membership.membership_dates,
														  :item => @membership,
														  :dates_url => membership_dates_url,
														  :events => %w{ Joined Left } } )

		  end
		}
	  else
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html(:errors, artist_date)
		  end
		}
	  end
	end
	
  end
  
  #
  #
  def update
    membership_date = MembershipDate.find(params[:id])
    @membership = Membership.find(params[:edit_date][:id])
	new_day = params[:edit_date][:day]
	new_month = params[:edit_date][:month]
	new_year = params[:edit_date][:year]
	
	if membership_date.day != new_day || membership_date.month != new_month || membership_date_year != new_year
	  new_date = FullDate.find(:first, :conditions => ["day = ? AND month = ? AND year = ?",
                                                      new_day, new_month, new_year])
	  
	  #
	  if new_date.nil?
	    membership_date.build_full_date(:day => new_day,
	                                    :month => new_month,
			  						    :year => new_year)
	  else
	    membership_date.full_date = new_date
	  end
	end
	
#
	respond_to do |format|
	  if membership_date.save
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html("membership-dates", :partial => 'dates/dates',
											 :locals => { :dates => @membership.membership_dates,
														  :item => @membership,
														  :dates_url => membership_dates_url,
														  :events => %w{ Joined Left } } )

		  end
		}
	  else
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html(:errors, artist_date)
		  end
		}
	  end
	end
  end
  
  #
  #
  def destroy
    bad_date = MembershipDate.find(params[:id])
	@membership = Membership.find(bad_date.membership_id)
	
	bad_date.delete
	
	#entry = Hash.new
	#entry.store("deleted date", [@artist.name])
	#Edit.create_edit(entry, @artist)
	
	respond_to do |format|
	  format.html {redirect_to_request_uri}
	  format.js {
	    render :update do |page|
		    page.replace_html("membership-dates", :partial => 'dates/dates',
										     	  :locals => { :dates => @membership.membership_dates,
													     	   :item => @membership,
														       :dates_url => membership_dates_url,
														       :events => %w{ Joined Left } } )
		  end
	  }
	end
  end
  
end
